package schoolword;

import java.util.Random;

public class Bank1 {

    private static double balance = 0;
    private static boolean hasTransaction = false;
    private static double transactionAmount;
    private static boolean isDeposit;

    public static void main(String[] args) {
        Producer producer = new Producer();
        Consumer consumer = new Consumer();

        new Thread(producer).start();
        new Thread(consumer).start();
    }

    static class Producer implements Runnable {

        @Override
        public void run() {
            Random random = new Random();
            while (true) {
                double amount = random.nextDouble() * 100;
                isDeposit = random.nextBoolean();

                transactionAmount = amount;
                hasTransaction = true;

                try {
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (Bank1.class) {
                    if (isDeposit) {
                        balance += transactionAmount;
                        System.out.println("存入金额是:" + transactionAmount + ",当前余额是:" + balance);
                    } else {
                        if (balance >= transactionAmount) {
                            balance -= transactionAmount;
                            System.out.println("取款金额是:" + transactionAmount + ",当前余额是:" + balance);
                        } else {
                            System.out.println("取款金额是:" + transactionAmount + ",当前余额是:" + balance + ",余额不足");
                            try {
                                wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    hasTransaction = false;
                    Bank1.class.notify(); // 存入后唤醒消费者线程
                }
            }
        }
    }

    static class Consumer implements Runnable {

        @Override
        public void run() {
            while (true) {
                synchronized (Bank1.class) {
                    if (hasTransaction) {
                        if (isDeposit) {
                            balance += transactionAmount;
                            System.out.println("存入金额是:" + transactionAmount + ",当前余额是:" + balance);
                        } else {
                            if (balance >= transactionAmount) {
                                balance -= transactionAmount;
                                System.out.println("取款金额是:" + transactionAmount + ",当前余额是:" + balance);
                            } else {
                                System.out.println("取款金额是:" + transactionAmount + ",当前余额是:" + balance + ",余额不足");
                                try {
                                    wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        hasTransaction = false;
                    }
                }
            }
        }
    }
}
